<?php
/**
 * Created by PhpStorm.
 * User: yaoihu
 * Date: 2019/6/14
 * Time: 23:56
 */

namespace DC\BonusCenter\Services;


use App\Models\Rule\BonusRuleModel;
use App\Models\Shop\UserAchievementModel;
use Carbon\Carbon;
use DC\BonusCenter\CommonBonus;
use DC\UserCenter\Models\UserAuthModel;
use DC\UserCenter\Models\UserBaseModel;
use App\Models\Bonus\BonusCountModel;
use DC\UserCenter\Models\UserInfoModel;
use DC\UserCenter\Models\UserNodeModel;
use Illuminate\Support\Facades\DB;

class UserApiBonus extends CommonBonus
{


    /**
     * 入网确定位置
     * @param $active_uid
     *
     */
    public function in_network($active_uid){

        $zhixiao_type = 2;//双轨或三轨  //此处后期需要调用配置
        $active_userinfo=UserBaseModel::where('member_id',$active_uid)->first();

        $pinfo=$this->get_pinfo($zhixiao_type,$active_userinfo);
        $usernode=new UserNodeModel();
        if(empty($pinfo)){
            $usernode->addnodefirst($active_userinfo);
        }else{
            $usernode->addnode($zhixiao_type,$active_userinfo,$pinfo);
        }
    }
    /**
     * 确定节点位置
     */
    public function get_pinfo($zhixiao_type,$active_userinfo){
        //该项目是推荐两人放右区
        $nodes = UserNodeModel::count();
        if($nodes>0){
            //推荐人信息
            $tj_user=UserBaseModel::where('member_id',$active_userinfo->referee_id)->first();

            $tj_pinfo=DB::table("plus_hjd_user_node")->where(['user_id'=>$active_userinfo->tuid,'status'=>1])->first();
            if(empty($tj_pinfo)){
                $pinfo='';//没有网头
            }else{
                if($tj_pinfo->cnum<$zhixiao_type){
                    $pinfo=$tj_pinfo;//推荐人的位置就是节点人的位置
                }else{
                    $pinfo=DB::table("plus_hjd_user_node")->where(['pids'=>['like',"%".$tj_pinfo['id']."%"],'status'=>1,'cnum'=>['lt',$zhixiao_type]])->order('y_pos ASC,cnum ASC,id ASC')->first();
                }
            }


        }else{
            //第一个进网体
            $pinfo='';//没有网头
        }
        return $pinfo;
    }

}